import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Shapes 1.15
import QtQuick.Layouts 1.15

MainView {
    id: root
    width: 800
    height: 391
    property color btnColor: "#7080C0"
    property int btnHeight: 50
    property int btnWidth: 125
    signal startLoad
    function getDeviceName(){
        var devices=[]
        if(mymodbus.axisCount===0){
            devices.push(qsTr("设备"))
        }
        else{
            for(let i=0; i<mymodbus.axisCount; i++){
                devices.push(qsTr("轴")+(i+1).toString()+"-"+qsTr("右"))
            }
            for(let i=0; i<mymodbus.axisCount; i++){
                devices.push(qsTr("轴")+(i+1).toString()+"-"+qsTr("左"))
            }
        }
        return devices;
    }
    onStartLoad: {
        singleSetLoader.visible = true
        deviceIndex.model = getDeviceName();
        decimalPlaces.currentIndex = decimalPlaces.find(mymodbus.getDecimalPlaces(0))
        minGraduation.currentIndex = minGraduation.find(mymodbus.getMinGraduation(0))
        maxRange.text = mymodbus.getMaxRange(0).toString()
        singleSetLoader.visible = false
    }
    Component.onCompleted: {
        startLoad()

    }
    Rectangle{
        id: mainRec
        width: root.width
        height: 280
        color: "transparent"
        MyComboBox
        {
            id:deviceIndex
            x: 324
            y: 46
            width: 160
            height: 30
            borderColor:"white"
            fillColor:"#0A3653"
            // background: {
            //     Rectangle: {
            //         color:"transparent"
            //     }
            // }

            model:[qsTr("仪表"),"4G"]
            currentIndex: 0
            onActivated: {
                decimalPlaces.currentIndex = decimalPlaces.find(mymodbus.getDecimalPlaces(deviceIndex.currentIndex))
                minGraduation.currentIndex = minGraduation.find(mymodbus.getMinGraduation(deviceIndex.currentIndex))
                maxRange.text = mymodbus.getMaxRange(deviceIndex.currentIndex).toString()
            }
        }
        GridLayout{
                        id: normalSettingsLayout
                        rows: 1
                        columns: 6
                        anchors.centerIn: parent
                        rowSpacing: 40
                        Button
                        {
                            Layout.row:0
                            Layout.column:0
                            Layout.preferredWidth: btnWidth
                            Layout.preferredHeight: btnHeight
                            text: qsTr("小数点位数")
                            font.pixelSize: 18
                            background: Rectangle{
                                color: btnColor
                                    }
                            onClicked: {
                                decimalPlaces.popup.open()
                            }
                        }
                        MyComboBox
                        {
                            id:decimalPlaces
                            Layout.row:0
                            Layout.column:1
                            Layout.preferredWidth: btnWidth
                            Layout.preferredHeight: btnHeight
                            model:[0,1,2,3,4]
                            //currentIndex: mymodbus.decimalPlaces
                        }
                            Button
                            {
                                Layout.row:0
                                Layout.column:2
                                Layout.preferredWidth: btnWidth
                                Layout.preferredHeight: btnHeight
                                // x: 152
                                // y: 13
                                text: qsTr("最小分度")
                                font.pixelSize: 18
                                background: Rectangle{
                                    color: btnColor
                                        }
                                onClicked: {
                                    //miniKeyBoard.lineValue = "";
                                    //miniKeyBoard.textInput=oldPwd;
                                    minGraduation.popup.open()
                                }
                            }
                            MyComboBox
                            {
                                id:minGraduation
                                Layout.row:0
                                Layout.column:3
                                Layout.preferredWidth: btnWidth
                                Layout.preferredHeight: btnHeight
                                model:[1, 2, 5, 10, 20, 50]
                                //currentIndex: mymodbus.minGraduation
                            }
                            Button
                            {
                                Layout.row:0
                                Layout.column:4
                                Layout.preferredWidth: btnWidth
                                Layout.preferredHeight: btnHeight
                                // x: 152
                                // y: 13
                                text: qsTr("最大量程")
                                font.pixelSize: 18
                                background: Rectangle{
                                    color: btnColor
                                        }
                                onClicked: {
                                    //miniKeyBoard.lineValue = "";
                                    //miniKeyBoard.textInput=oldPwd;
                                   //rectangle.selectedView=16
                                   enterNumberPanel.lastButtonIndex = 4;
                                   enterNumberPanel.lastViewIndex =8;
                                   enterNumberPanel.minimum=0;
                                   enterNumberPanel.clearText()
                                   enterNumberPanel.maximum=100000*mymodbus.minGraduation>999999?999999:100000*mymodbus.minGraduation
                                   enterNumberPanel.itemName=qsTr("最大量程")
                                   enterNumberPanel.itemValue=maxRange.text
                                   enterNumberPanel.textInput=maxRange;

                                    enterNumberPanel.visible=true;
                                    enterNumberPanel.lastRec=mainRec
                                    mainRec.visible=false
                                }
                            }
                            Rectangle {
                                id: recMaxRange
                                color: "transparent"
                                border.width: 0
                                border.color: "white"
                                Layout.row:0
                                Layout.column:5
                                Layout.preferredWidth: btnWidth
                                Layout.preferredHeight: btnHeight
                                // 绘制一条边框
                                Shape {
                                    anchors.fill: parent
                                    ShapePath {
                                        strokeWidth: 3
                                        strokeColor: "white"
                                        strokeStyle: ShapePath.SolidLine
                                        startX: 0
                                        startY: recMaxRange.height
                                        PathLine {
                                            x: recMaxRange.width
                                            y: recMaxRange.height
                                        }
                                    }
                                }
                                TextInput{
                                    id:maxRange
                                    anchors.fill: parent
                                    //echoMode: TextInput.Password
                                    //text:mymodbus.maxRange
                                    color:"white"
                                    font.pixelSize: 25
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignBottom
                                    MouseArea {
                                           anchors.fill: parent
                                           onClicked: {
                                               // miniKeyBoard.lineValue = "";
                                               // miniKeyBoard.textInput=oldPwd;
                                               //rectangle.selectedView=16
                                               enterNumberPanel.lastButtonIndex = 4;
                                               enterNumberPanel.lastViewIndex =8;
                                               enterNumberPanel.minimum=0;
                                               enterNumberPanel.clearText()
                                               enterNumberPanel.maximum=100000*mymodbus.minGraduation>999999?999999:100000*mymodbus.minGraduation
                                               enterNumberPanel.itemName=qsTr("最大量程")
                                               enterNumberPanel.itemValue=maxRange.text
                                               enterNumberPanel.textInput=maxRange;
                                               enterNumberPanel.visible=true;
                                               enterNumberPanel.lastRec=mainRec
                                               mainRec.visible=false
                                           }
                                       }

                                }
                            }

        }
    Button
        {
            id:btnZeroCalOK
            x: 572
            y: 281
            //x: 675
            //y: 20
            width: 100
            height: 100
            background: Rectangle{
                border.width: 0
                color: "#C070B0"
                    }

            text: qsTr("确定")
            font.pixelSize: 25
            palette.buttonText: "white"
            //Connections {
            //    target: btnZeroCalOK
                onClicked: {
                    //mymodbus.zeroCalibration()
                    //rectangle.selectedView = 2
                    //bottomBar.selected=2
                    if(singleSetLoader.visible){
                        return;
                    }
                    singleSetLoader.visible=true;
                    var result = mymodbus.setDecimalPlaces(decimalPlaces.currentValue, deviceIndex.currentIndex)
                            && mymodbus.setMinGraduation(minGraduation.currentValue, deviceIndex.currentIndex)
                            && mymodbus.setMaxRange(Number(maxRange.text), deviceIndex.currentIndex)
                    if(result){
                        zeroCalPanelDialog.title=qsTr("参数设置成功！")
                    }
                    else{
                        zeroCalPanelDialog.title=qsTr("参数设置失败！")

                    }
                    singleSetLoader.visible=false;
                    zeroCalPanelDialog.open();
    //                rectangle.selectedView = 2
    //                bottomBar.selected=2

                }
            //}
        }

        Button
        {
            id:btnReturn
            x: 688
            y: 281
            //x: 675
            //y: 20
            width: 100
            height: 100
            background: Rectangle{
                border.width: 0
                color: "#B0B0D0"
                    }

            text: qsTr("返回")
            font.pixelSize: 25
            palette.buttonText: "white"
            //Connections {
            //    target: btnReturn
                onClicked: {
                    //rectangle.selectedView = 2
                    bottomBar.selected=2
//                    myLoader.visible=false;
//                    myLoader.visible=true
                    myLoader.source="FunctionPanel.qml"
                }
            //}
        }

    }
        MyBuzyIndicator{
            anchors.centerIn: parent
            id: singleSetLoader
            visible:false
        }
        Dialog {
            anchors.centerIn: parent
            closePolicy: "CloseOnEscape"
            modal:true
            id: zeroCalPanelDialog
            standardButtons: Dialog.Ok
            onAccepted: {
                zeroCalPanelDialog.close()
                if(zeroCalOK){
                    bottomBar.selected=0
                    //myLoader.setsource="StatusView.qml"
                    //myLoader.visible=false;
                    //statusView.visible=true;
//                    myLoader.visible=false;
//                    myLoader.visible=true;
                    myLoader.source="StatusView.qml"
                }
            }
            z:100
        }
    EnterNumberPanel{
          id:enterNumberPanel
          width: 800
          height: 391
          visible:false
    }
}
